package cn.sciento.dd.config;

import java.nio.charset.StandardCharsets;
import cn.sciento.dd.service.DingCorpAddressService;
import cn.sciento.dd.service.DingCorpAgentManageService;
import cn.sciento.dd.service.DingCorpMessageService;
import cn.sciento.dd.service.DingFileStorageService;
import cn.sciento.dd.service.DingTicketService;
import cn.sciento.dd.service.DingTokenService;
import cn.sciento.dd.service.impl.DingCorpAddressServiceImpl;
import cn.sciento.dd.service.impl.DingCorpAgentManageServiceImp;
import cn.sciento.dd.service.impl.DingCorpMessageServiceImp;
import cn.sciento.dd.service.impl.DingCorpSyncServiceImpl;
import cn.sciento.dd.service.impl.DingFileStorageServiceImp;
import cn.sciento.dd.service.impl.DingTicketServiceImp;
import cn.sciento.dd.service.impl.DingTokenServiceImpl;
import cn.sciento.starter.integrate.service.AbstractCorpSyncService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
public class DingConfig {
  @Bean
  @ConditionalOnMissingBean(name = {"wdRedisTemplate"})
  public RedisTemplate wdRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    redisTemplate.setKeySerializer((RedisSerializer)new StringRedisSerializer());
    redisTemplate.setValueSerializer((RedisSerializer)new GenericJackson2JsonRedisSerializer());
    return redisTemplate;
  }
  
  @Bean
  @ConditionalOnMissingBean(name = {"wdRestTemplate"})
  public RestTemplate wdRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
    return restTemplate;
  }
  
  @Bean
  public AbstractCorpSyncService dingCorpSyncService() {
    return (AbstractCorpSyncService)new DingCorpSyncServiceImpl();
  }
  
  @Bean
  @ConditionalOnMissingBean
  public DingCorpMessageService dingCorpMessageService() {
    return (DingCorpMessageService)new DingCorpMessageServiceImp();
  }
  
  @Bean
  @ConditionalOnMissingBean
  DingFileStorageService dingFileStorageService() {
    return (DingFileStorageService)new DingFileStorageServiceImp();
  }
  
  @Bean
  @ConditionalOnMissingBean
  DingTicketService dingTicketService() {
    return (DingTicketService)new DingTicketServiceImp();
  }
  
  @Bean
  @ConditionalOnMissingBean
  DingCorpAddressService dingCorpAddressService() {
    return (DingCorpAddressService)new DingCorpAddressServiceImpl();
  }
  
  @Bean
  @ConditionalOnMissingBean
  DingCorpAgentManageService dingCorpAgentManageService() {
    return (DingCorpAgentManageService)new DingCorpAgentManageServiceImp();
  }
  
  @Bean
  @ConditionalOnMissingBean
  DingTokenService dingTokenService() {
    return (DingTokenService)new DingTokenServiceImpl();
  }
}
